<template>
	<view class="uni-numbox">
		<text style="padding-right:5px;padding-top:2px;color:#BBBBBB;font-size:12px;" v-if="showMsg"> {{msg}}  </text>
		<view class="uni-numbox__minus" :class="{'uni-numbox--disabled': inputValue <= min || disabled}" @click="_calcValue('minus')">-</view>
		 <input class="uni-numbox__value" type="number" disabled="true" v-model="inputValue" @blur="_onBlur">
		<view class="uni-numbox__plus" :class="{'uni-numbox--disabled': inputValue >= max || disabled}" @click="_calcValue('plus')">+</view>
	</view>
</template>
<script>
	export default {
		name: 'uni-number-box',
		props: {
			index:{
				type: [Number, String],
				default: 0
			},
			value: {
				type: [Number, String],
				default: 0
			},
			showMsg: {
				type: Boolean,
				default: true
			},
			msg: {
				type: String,
				default: ""
			},
			min: {
				type: Number,
				default: 0
			},
			max: {
				type: Number,
				default: 100
			},
			step: {
				type: Number,
				default: 1
			},
			stepStart: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			},
			disabledMsg: {
				type: String,
				default: ""
			},
			checkCount: {
				type: Number,
				default: -1
			},
			checkMsg: {
				type: String,
				default: ""
			}
		},
		data() {
			return {
				inputValue: 0
			}
		},
		watch: {
			value(val) {
				this.inputValue = +val;
			},
			inputValue(newVal, oldVal) {
				if (+newVal !== +oldVal) {
					this.$emit('change', { Index:this.index, Value:newVal } );
				}
			}
		},
		methods: {
			_calcValue(type) {
				if (this.disabled) {
					if(this.disabledMsg!="") uni.showToast({title:this.disabledMsg,icon:"none"});
					return
				}
				
				const scale = this._getDecimalScale()
				let value = this.inputValue * scale
				let step = this.step * scale
			
				if (type === 'minus') {
					if(value==this.stepStart){
						step = this.stepStart;		
					}
					value -= step
				} else if (type === 'plus') {
					if(value==0){
						step = this.stepStart;		
					}
					value += step
				}
				if (value < this.min) {
					return;
				}
				if(this.checkCount!=-1){
					if(value > this.checkCount){
						uni.showToast({title:this.checkMsg,icon:"none"});
						return;
					}
				}
				if (value > this.max) {
					if(this.msg!=""){
						if(this.msg.length>12){
							uni.showModal({
								title: "温馨提示",
								content:this.msg,
								showCancel: false,
								confirmText:"确定"
							});
						}else{
							uni.showToast({title:this.msg,icon:"none"});
						}
					} 
					return;
				}
				this.inputValue = value / scale;
			},
			_getDecimalScale() {
				let scale = 1
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, (this.step + '').split('.')[1].length)
				}
				return scale
			},
			_onBlur(event) {
				let value = event.detail.value
				if (!value) {
					this.inputValue = 0
					return
				}
				value = +value;
				if (value > this.max) {
					value = this.max
				} else if (value < this.min) {
					value = this.min
				}
				this.inputValue = value
			}
		},
		created() {
			this.inputValue = +this.value;
		}
	}
</script>
<style lang="scss" >
	@charset "UTF-8";

	.uni-numbox {
		display: inline-flex;
		flex-direction: row;
		justify-content: flex-start;
		height: 60upx;
		position: relative
	}

	

	.uni-numbox__minus,
	.uni-numbox__plus {
		margin: 0;
 	    background-color: $uni-theme-color;
	    color: #ffffff;
		width: 60upx;
		font-size: 40upx;
		height: 100%;
		line-height: 60upx;
		text-align: center;
		position: relative
	}
	.uni-numbox__minus{
	  border-top-left-radius:  12upx;
		border-bottom-left-radius: 12upx;
	}
	.uni-numbox__plus{
	  border-top-right-radius: 12upx;
		border-bottom-right-radius: 12upx;
	}


	.uni-numbox__value {
		position: relative;
		background-color: #f5f5f5;
		width: 60upx;
		height: 100%;
		text-align: center;
		padding: 0
	}

	.uni-numbox__value:after {
		content: '';
		position: absolute;
		transform-origin: center;
		box-sizing: border-box;
		pointer-events: none;
		top: -50%;
		left: -50%;
		right: -50%;
		bottom: -50%;
		border-style: solid;
		border-color: #f5f5f5;
		border-left-width: 1px;
		border-right-width: 1px;
		border-top-width: 0;
		border-bottom-width: 0;
		transform: scale(.5)
	}

	.uni-numbox--disabled {
		color: silver
	}
</style>